<?php

namespace app\api\controller\yexam;

use app\admin\model\yexam\QuestionLog;
use app\common\controller\Api;

/**
 * @ApiSector (试题相关)
 * @ApiWeight (100)
 */

class Note extends Api
{
    protected $noNeedRight = ["*"];
    protected $noNeedLogin = [];

    /**
     * @ApiTitle (添加笔记和获取单个笔记内容)
     * @ApiMethod (GET)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     * @ApiParams (name=content,type=string,required=true,description=笔记内容)
     * @ApiParams (name=question_id,type=integer,required=true,description=试题id)
     * @ApiParams (name=subject_id,type=integer,required=true,description=科目id)
     * @ApiParams (name=type,type=string,required=true,description=默认为get，获取,put=增加)
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addNote()
    {
        $note = new \app\admin\model\yexam\Note();
        $content = $this->request->param('content');
        $questionId = $this->request->param('question_id');
        $subjectId = $this->request->param('subject_id');
        $type = $this->request->param('type','get');
        $isAddNote = $note->where('user_id',$this->auth->id)->where('question_id',$questionId)->find();
        if ($type == 'get')
        {
            if (!is_null($isAddNote)) $this->success('获取成功',$isAddNote->content);
            else $this->success('','');
        }
        if ($type == 'put')
        {
            if (is_null($isAddNote))
            {
                $unit_id= \app\admin\model\yexam\Question::get($questionId)->unit_id;
                $note->content = $content;
                $note->user_id = $this->auth->id;
                $note->question_id = $questionId;
                $note->subject_id = $subjectId;
                $note->add_time = time();
                $note->unit_id = $unit_id;
                $note->save();
                $this->success('笔记添加成功');
            }else
            {
                $isAddNote->content = $content;
                $isAddNote->save();
                $this->success('笔记修改成功');
            }
        }

    }

    /**
     * @ApiTitle (获取笔记列表)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNoteList(){
        $uid = $this->auth->id;
        $note = new \app\admin\model\yexam\Note();
        $subject_id = $this->request->post('subject_id');
        $device = $this->request->post('device');
        $unitId = $this->request->post('unit_id',0);
        $page = $this->request->post('page');
        $limit = $this->request->post('limit');
        $question = $this->request->post('question');
        if ($device == 'pc')
        {
            $where['user_id'] = $uid;
            $where['subject_id'] = $subject_id;
            if (!empty($question))
            {
                $data = $note->where($where)->field('question_id,content,id')->page($page,$limit)->select();
                foreach ($data as $key => $va)
                {
                    $data[$key] = \app\admin\model\yexam\Question::get($va['question_id']);
                    $data[$key]['content'] = $va['content'];
                    $data[$key]['id'] = $va['id'];
                }
                $total = $note->where($where)->count();
                $this->success('',[
                    'data' => $data,
                    'total' => $total
                ]);
            }
            $data = $note->where($where)->field('count(id) as num,unit_id')
                ->group('unit_id')->select();
            foreach ($data as $key => $va)
            {
                $data[$key]['unit_name'] = \app\admin\model\yexam\Unit::get($va['unit_id'])->unit_name;
            }
            $this->success('',[
                'data' => $data
            ]);
        }
        $list = $note->where('user_id',$uid)->where('subject_id',$subject_id)->order('add_time desc')->select();
        $question = new \app\admin\model\yexam\Question();
        $unit = new \app\admin\model\yexam\Unit();
        foreach ($list as $key => $value)
        {
            $letQuestion = $question->where('id',$value['question_id'])->find();
            $list[$key]['title'] = $letQuestion->question_name;
            $list[$key]['source'] = $unit->where('id',$letQuestion->unit_id)->value('unit_name');
            $list[$key]['answer'] = $value['content'];
            $list[$key]['interpret'] = $letQuestion->right_answer;
            $list[$key]['isOpen'] = false;
            $list[$key]['text'] = "移除笔记";
            $list[$key]['style'] = "primary";
        }
        $this->success('',$list);
    }

    /**
     * @ApiTitle (移除笔记)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     * @ApiParams (name=ids,type=string,required=true,description=笔记id)
     * @ApiReturnParams (name=msg,type=string,required=true,sample=移除3条笔记)
     * @ApiReturn ({msg: "移除了3条笔记"})
     */
    public function removeNotes()
    {
        $ids = $this->request->param('ids');
        $note = new \app\admin\model\yexam\Note();
        $res = $note->where('id','in',$ids)->delete();
        $this->success('移除'.$res."条笔记");
    }
    /**
     * @ApiTitle (笔记答题卡)
     * @ApiMethod (POST)
     * @ApiParams (name=unit_id,type=integer,required=true,description=章节id)
     */
    public function note_card(){
        $unitId = $this->request->param('unit_id');
        $user_id = $this->auth->id;
        $note = new \app\admin\model\yexam\Note();
        $data = collection($note
            ->alias("log")
            ->field("question.id")
            ->join("yexam_question question","log.question_id=question.id",'inner')
            ->join("yexam_unit unit","log.unit_id=unit.id",'inner')
            ->where(['log.unit_id'=>$unitId,'log.user_id'=>$user_id])
            ->order("question.type asc,question.id asc")
            ->select());
        $this->success('请求成功',$data);
    }
}